<?php


namespace app\admin\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Seckill2 extends Command
{
    protected function configure()
    {
        $this->setName('Seckill2')->setDescription('秒杀使用恢复价格');
    }

    protected function execute(Input $input, Output $output)
    {
        //获取当前秒杀时间
        $data_list = Db::name('xsms_time')
            ->where([
                'status' => 1,
                'is_del' => 0
            ])
            ->order('start_time')
            ->select();
        foreach ($data_list as $k => $v) {
            $start_time = strtotime($v['start_time']);
            $end_time = $start_time + ($v['delayed_time'] * 60);
            $data_list[$k]['start_time'] = $start_time;
            $data_list[$k]['end_time'] = $end_time;
            //在秒中的
            //goods is_ms 是否正在秒杀 1是 2不是
            if (time() >= $start_time && time() <= $end_time) {
                $ms_data = $v;
            }
        }
        if ($ms_data) {
            $goods_update = Db::name('goods')->where([
                [
                    $where[] = ['is_del', '=', 0],
                    $where[] = ['goods_area', '=', 1],
                    $where[] = ['ms_id', '=', $ms_data['id']],
                ]
            ])->update([
                'is_ms' => 1
            ]);
            $goods_update = Db::name('goods')->where([
                [
                    $where[] = ['is_del', '=', 0],
                    $where[] = ['goods_area', '=', 1],
                    $where[] = ['ms_id', '<>', $ms_data['id']],
                ]
            ])->update([
                'is_ms' => 2
            ]);
        }else{
            $goods_update = Db::name('goods')->where([
                [
                    $where[] = ['is_del', '=', 0],
                    $where[] = ['goods_area', '=', 1],
                ]
            ])->update([
                'is_ms' => 2
            ]);
        }

        $output->writeln('秒杀操作完成');
    }


}